$(function () {
  var swiper = new Swiper(".mySwiper", {
    slidesPerView: 1,
    spaceBetween: 30,
    loop: true,
    autoplay: true,
  });

  var Toast = $(".mhy-toast");
  Toast.hide();

  var shopBtnsBox = $(".shop-btns");
  axios({
    url: `http://localhost:6766/api/goods`
  }).then(function (res) {
    var newData = res.data.datas;
    $(newData).each(function (index, item) {
      shopBtnsBox.append(`
            <div class="shop-btn" data-id="${item.id}"></div>
      `);
    })

    var shopBtns = shopBtnsBox.children();
    shopBtns.click(function () {
      if ($.cookie('username') != null) {
        var username = $.cookie("username");
        axios({
          url: `http://localhost:6766/api/cart`,
          method: "post",
          data: {
            "id": this.dataset.id,
            "username": username
          }
        }).then(function (res) {
          Toast.show();
          var mytime = setTimeout(function () {
            Toast.hide();
            clearTimeout(mytime);
          }, 1000);
        })
      } else {
        location.href = "./login.html"
      }
    })
  })

})